const Core = require('@alicloud/pop-core');//引入阿里模块
const rn = require("randomnumber");//引入随机数模块
const jwt = require("jsonwebtoken");//引入token模块
module.exports = {
    sendMsg(tel,code){
        var client = new Core({
            accessKeyId: 'LTAI4GDzA23tDeC29ANS2ksa',
            accessKeySecret: 'vL5jNJmFk1QMa2szi2EHSY0fKRCdmx',
            endpoint: 'https://dysmsapi.aliyuncs.com',
            apiVersion: '2017-05-25'
          });
          
          var params = {
            "RegionId": "cn-hangzhou",
            "PhoneNumbers": tel,
            "SignName": "呆小呆",
            "TemplateCode": "SMS_204277934",
            "TemplateParam": `{code:${code}}`

          }
          
          var requestOption = {
            method: 'POST'
          };
          return new Promise((resolve,reject)=>{//利用promise返回发送短信的结果
            client.request('SendSms', params, requestOption).then((result) => {
                console.log(JSON.stringify(result));
                resolve();
              }, (ex) => {
                console.log(ex);
                reject();
              })    
          })
    },
    randomNum(n){
        return rn.generate(n);//利用随机数模块产生一个n位的随机数
    },
    verify(t){
      return new Promise((resolve,reject)=>{
        jwt.verify(t,"乾坤未定，你我皆是黑马",(err,data)=>{
          console.log(typeof err)
          if(err) reject();
          resolve(data);
        })
      })
    }
}
